<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class MerchandiseController extends CoreController {
    
	public function index(){
        if (I('get.p')) {
            $page=I('get.p');
        }else{
            $page=1;
        }
        if (I('get.pid')) {
            $pid=I('get.pid');
            $MerchandiseModel = M('Merchandise');
            $count=M('Merchandise')->join('left join fm_merchandise_category on fm_merchandise.id=fm_merchandise_category.merchandise_id')->where('fm_merchandise_category.category_id='.$pid)->count();

            $arr=M('Merchandise')->join('left join fm_merchandise_category on fm_merchandise.id=fm_merchandise_category.merchandise_id')->where('fm_merchandise_category.category_id='.$pid)->page($page.',2')->select();
            $this->assign('list',$arr);
        }else{
            $MerchandiseModel = M('Merchandise');
            $MerchandiseList = $MerchandiseModel->order('id desc')->page($page.',2')->select();
            $this->assign('list',$MerchandiseList);
            
            $count = $MerchandiseModel->count();
            } 
            $this->Page = new \Think\Page($count,2);
            $this->Page->setConfig('prev','上一页');
            $this->Page->setConfig('next','下一页');

            $this->assign('addText','添加商品');
            $this->assign('addNew',U('addMerchandise'));
            $this->assign('edit',U('editMerchandise'));
            $this->assign('del',U('delMerchandise'));
            $this->display();        
    }

    public function addMerchandise(){
        if(IS_POST){
            //上传商品
            $data = array(
                'merchandise' => I('post.merchandise'),
                'numbers' => I('post.numbers'),
                'stock' => I('post.stock'),
                'price' => I('post.price'),
                'keywords' => I('post.keywords'),
                'description' => I('post.description'),
                'details' => I('post.details'),
                'status' => I('post.status',0,'intval'),
                'add_time'=>date('Y-m-d H:i:s',time()),
                );
            $merchandiseInfo = M('Merchandise')->where("merchandise='".I('post.merchandise')."'")->find();
            if(!$merchandiseInfo){
                $merchandiseId = M('Merchandise')->add($data);
                //上传分类
                $Merchandise_categoryModel= M('Merchandise_category');

                foreach (I('post.category_id') as $key => $value) {
                    $cate[]=array('merchandise_id'=>$merchandiseId,'category_id'=>$value);
                }
                $Merchandise_categoryModel->addAll($cate);
                //上传图片
                $PhotoModel=M('Photos');

                $photo='';
                foreach (I('post.photos') as $key => $value) {
                    $photo[$key]['photos']=$value;
                }
                foreach (I('post.descriptions') as $key => $value) {
                    $photo[$key]['descriptions']=$value;
                }
                foreach (I('post.link') as $key => $value) {
                    $photo[$key]['link']=$value;
                    $photo[$key]['merchandise_id']=$merchandiseId;
                }

                $PhotoModel->addAll($photo);

                header('Location:'.U('index'));         
            }else{
                $this->error('商品已存在');
            }

        }else{
            $this->assign('cancel',U('index'));
            $this->optionStr=$this->getCateOptionHtml();
            $this->display();  
        }    
    }
    public function editMerchandise(){
        if (IS_POST) {
            $id = I('post.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
            $data = array(
                'merchandise' => I('post.merchandise'),
                'numbers' => I('post.numbers'),
                'stock' => I('post.stock'),
                'price' => I('post.price'),
                'keywords' => I('post.keywords'),
                'description' => I('post.description'),
                'details' => I('post.details'),
                'status' => I('post.status',0,'intval'),
                'add_time'=>date('Y-m-d H:i:s',time()),
                );
            $MerchandiseModel = M('Merchandise');
            $merchandiseInfo = M('Merchandise')->where("id=".$id)->save($data);

            $photo='';
                foreach (I('post.photos') as $key => $value) {
                    $photo[$key]['photos']=$value;
                }
                foreach (I('post.descriptions') as $key => $value) {
                    $photo[$key]['descriptions']=$value;
                }
                foreach (I('post.link') as $key => $value) {
                    $photo[$key]['link']=$value;
                    $photo[$key]['merchandise_id']=$id;
                }

            M('Photos')->where("merchandise_id=".$id)->delete();
            M('Photos')->addAll($photo);

            foreach (I('post.category_id') as $key => $value) {
                    $cate[]=array('merchandise_id'=>$id,'category_id'=>$value);
                }

            M('Merchandise_category')->where("merchandise_id=".$id)->delete();
            M('Merchandise_category')->addAll($cate);
            
            header('Location:'.U('index')); 
        }else{
            $id = I('get.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
            $merchandise = M('Merchandise')->where("id='".I('get.id')."'")->find();
            $category = M('Merchandise_category')->where("merchandise_id='".I('get.id')."'")->select();
            foreach ($category as $key => $value) {
                $cates[] = $value['category_id'];
            }
            $this->optionStr=$this->getCateOptionHtml($cates);

            $photo=M('Photos')->where("merchandise_id='".I('get.id')."'")->select();

            $this->assign('merchandise',$merchandise);
            $this->assign('photo',$photo);
            $this->display(); 
        }

    }
    public function delMerchandise(){
        $id = I('get.id',0,'intval');
        if($id<1){
            $this->error('错误的ID');
        }
        $MerchandiseModel = M('Merchandise');
        $MerchandiseModel->delete($id);
        M('Merchandise_category')->where("merchandise_id=".$id)->delete();
        M('Photos')->where("merchandise_id=".$id)->delete();
        $this->success('删除成功','index');
    }
}